package linkedlist;

/**
 * Created with IntelliJ IDEA.
 * Description: 142.环形链表
 * User: Li_yizYa
 * Date: 2025—03—25
 * Time: 22:31
 */
public class Solution6 {
    private static ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        boolean flag = false;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            return null;
        }
        fast = head;
        while (fast != slow) {
            slow = slow.next;
            fast = fast.next;
        }
        return fast;
    }
}
